﻿using System;
using System.Collections.Generic;
using System.Text;

namespace GeoFly
{
    /// <summary>
    /// 反距离权重插值法，使用距离权重的指数为2次方
    /// </summary>
    public class IDWInter:SpatialInterpolate
    {        
        /// <summary>
        /// 得到指定日期的栅格图层插值结果
        /// </summary>
        /// <param name="date">指定的日期</param>
        /// <param name="exp">幂指数</param>
        /// <param name="bOutput">中间结果是否输出</param>
        public GridLayer SpatialGridOut(DateTime date)
        {
            if (this.pStationinfo == null || this.pMeteoData == null)
                return null;
            
            //栅格数据结构初始化
            m_gridLayer = HydroSimulate.g_GridLayerPara.g_DemLayer.AttributesCopy();
            
            ProgressBar bar = new ProgressBar();
            bar.Show();
            bar.Text = "正在内插栅格数据";
            if (this.m_CoordType == CoordType.UTM_Coord)
            {
                this.PrepareFromUTM(date);
            }
            else
            {
                this.PrepareFromLongLa(date);
            }
            for (int row = 0; row < m_gridLayer.rowCount; row++)		//开始新一日的空间插值计算
            {
                for (int col = 0; col < m_gridLayer.colCount; col++)
                {
                    double value = -9999;
                    //获取当前计算栅格的坐标
                    LPoint p = m_gridLayer.CellPosition(row, col);
                    double sum1 = 0;
                    double sum2 = 0;
                    bool IsOnStation = false;
                    for (int i = 0; i < this.pStationinfo.Count; i++)
                    {
                        double distX = X[i] - p.X;
                        double distY = Y[i] - p.Y;
                        double distSqare = distX * distX + distY * distY;
                        //在站点上
                        if (distSqare < 0.00001)
                        {
                            value = Z[i];
                            IsOnStation = true;
                            break;
                        }
                        sum1 += Z[i] / distSqare;
                        sum2 += 1 / distSqare;
                    }
                    //不在站点上
                    if(IsOnStation==false)
                        value = sum1 / sum2;
                    this.m_gridLayer[row, col] = value;

                }
                bar.progressBar1.Value = (int)(row * 100.0 / m_gridLayer.rowCount);
            }
            //图层中间过程输出
            bar.Close();
            return this.m_gridLayer;
        }
        
    }
}
